const crypto = require('crypto')
const jwt = require('jsonwebtoken')
const { PRIVATE_KEY } = require("./constant")

// 判断是不是对象
function isObject(o) {
    return Object.prototype.toString.call(o) === '[object Object]'
}

function md5(s) {
    return crypto.createHash('md5').update(String(s)).digest('hex');
}
// token解析出username  JWT
function decoded(req){
    const authorization = req.get('Authorization')
    let token = ''
    if (authorization.indexOf('Bearer') >= 0) {
        token = authorization.replace('Bearer ', '')
    } else {
        token = authorization
    }
    console.log(token)
    return jwt.verify(token, PRIVATE_KEY)
}

module.exports= {
    md5,
    decoded,
    isObject
}